# -*- coding： UTF-8 -*-
from random import  randint

def getMinCommonMultiple(x,y):
    if x == y: return  x

    if x > y :
       if x % y == 0 : return  x
    else:
       if y % x == 0 : return y
    mcd = getMaxCommonDivisor(x,y)
    return  x * y / mcd


def getMaxCommonDivisor(x,y):
    if x == y: return x

    if x > y:
        if x % y == 0: return y
    else:
        if y % x == 0: return x

    mcd = y

    while 0 != ( x % y ):
        mcd = x % y
        x , y =  y , mcd

    return  mcd

def randomFraction(range):
    a = 1
    while a == 1:
        a = randint(1,range)
    b = randint(1,a-1)
    if randint(0,1) == 0 :
        return  str(b) + "/" + str(a)
    else:
        c = randint(1,range)
        return  str(c) + "'" + str(b)  + "/" + str(a)

def fractionAdd(a,b):
    if a[1] == b[1]:
        return  [a[0]+b[0],a[1]]
    denominator = getMinCommonMultiple(a[1],b[1])
    x = denominator / a[1]
    y = denominator / b[1]
    molecule = a[0] * x  + b[0] * y
    return [molecule,denominator]


def fractionSubtraction(a,b):
    if a[1] == b[1]:
        return  [a[0]-b[0],a[1]]
    denominator = getMinCommonMultiple(a[1],b[1])
    x = denominator / a[1]
    y = denominator / b[1]
    molecule =(a[0] * x) - (b[0] * y)
    return [molecule,denominator]

def fractionDivision(a,b):
    return [a[0] * b[1],a[1]*b[0]]

def fractionMultiply(a,b):
    return [a[0]*b[0],a[1]*b[1]]

def transToPostExp(expression):
    postExp = []
    ops = []
    for item in expression:
        if item == '(':
            ops.append(item)
        elif item in ('+','-'):
            while len(ops) != 0:
                op = ops.pop()
                if op == '(' :
                    ops.append(op)
                    break
                postExp.append(op)
            ops.append(item)
        elif item in ('*','÷'):
            while len(ops) != 0:
                op = ops.pop()
                if op in ('(','+','-'):
                    ops.append(op)
                    break
                else:
                    postExp.append(op)
            ops.append(item)
        elif item == ')':
            while len(ops) != 0:
                op = ops.pop()
                if op == '(':
                    break
                else:
                    postExp.append(op)
        else:
            postExp.append(item)

    while len(ops) != 0:
        op = ops.pop()
        postExp.append(op)

    return postExp

def computePostExpValue(expression):
    for i in expression:
        if type(i) == int:
            continue
        elif i == '÷':
            return computePostExpWithFraction(expression)
        elif '/' in i:
            return  computePostExpWithFraction(expression)
    nums = []
    for item in expression:
        if item == '+':
            a =  int(nums.pop())
            b = int(nums.pop())
            c = a + b
            nums.append(c)
        elif item == '-':
            a = int(nums.pop())
            b = int(nums.pop())
            c = b - a
            nums.append(c)
        elif item == '*':
            a = int(nums.pop())
            b = int(nums.pop())
            c = a * b
            nums.append(c)
        elif item == '÷':
            a = int(nums.pop())
            b = int(nums.pop())
            if a  == 0 :
                return "Error"
            else:
                c = b / a
                nums.append(c)
        else:
            nums.append(item)

    return nums.pop()




def computePostExpWithFraction(expression):
    fraction = []
    #[分子，分母]
    for item in expression:
        if item == '+':
            a = fraction.pop()
            b = fraction.pop()
            c = fractionAdd(a,b)
            fraction.append(c)
        elif item == '-':
            a = fraction.pop()
            b = fraction.pop()
            c = fractionSubtraction(b,a)
            fraction.append(c)
        elif item == '*':
            a = fraction.pop()
            b = fraction.pop()
            c = fractionMultiply(a,b)
            fraction.append(c)
        elif item == '÷':
            a = fraction.pop()
            b = fraction.pop()
            c = fractionDivision(b,a)
            fraction.append(c)
        else:
            if type(item)== str and '/' in item:
                slash = item.index("/")
                if "'" in item:
                    quote = item.index("'")
                    integer = int(item[0:quote])
                    molecule = int(item[quote + 1 : slash])
                    denominator = int(item[slash+1:])
                    molecule = integer * denominator +  molecule
                    fraction.append([molecule,denominator])
                else:
                    denominator = int(item[slash + 1:])
                    molecule =int(item[0:slash])
                    fraction.append([molecule, denominator])
            else:
                fraction.append([int(item),1])
    res = fraction.pop()
    #[整数部分,分子，分母]
    #分子为0时直接输出整数
    div = getMaxCommonDivisor(res[0],res[1])
    return  [int(res[0]/res[1]),int((res[0]%res[1])/div),int(res[1]/div)]